$(function () {
    //分类更多
    sortMoreFun();
    listData(1);
    /**
     * 所有专业和科目
     */
    findSubjectList(function (data) {
        var parentSubjectList = data[0];
        if (parentSubjectList != null && parentSubjectList.length > 0) {
            var context = '<li data-id="" class="cm-filter-item parent-subject active">全部</li>';
            $.each(parentSubjectList, function (i, e) {
                context += '<li data-id="' + e.id + '" class="cm-filter-item parent-subject">' + e.subjectName + '</li>';
            });
            $("#subject-list-parent").html(context);
            sortMoreFun();
            //点击父级专业
            $("body").on('click', ".parent-subject", function () {
                var _this = $(this);
                _this.addClass('active').siblings().removeClass('active');
                var parentId = _this.data('id');
                $("#subjectId").val(parentId);
                listData(1);
                var childList = data[parentId];
                if (childList != null && childList.length > 0) {
                    $("#child-subject-list").removeClass('hide');
                    context = '<li data-id="' + parentId + '" class="cm-filter-item active">全部</li>';
                    $.each(childList, function (index, em) {
                        context += '<li class="cm-filter-item" data-id="' + em.id + '">' + em.subjectName + '</li>';
                    });
                    $("#child-subject-list ul").html(context);
                    //sortMoreFun();
                } else {
                    $("#child-subject-list").addClass('hide');
                }
            });
        }
    });

    $('body').on('click', '#child-subject-list li', function () { //科目的点击事件
        selectSubSubject($(this));
    }).on('click', 'span[data-order]', function () {
        selectOrder($(this));
    }).on('click', '[data-details]', function () {
        bookDetails($(this));
    }).on("click", ".book_buy",function(){
        var stockNum = $(this).data('stocknum');
        var bookName = $(this).data('bookname');
        if (stockNum<1) {
            layer.msg("图书【"+bookName+"】,当前库存【"+stockNum+"本】，库存不足")
            return;
        }
        window.location.href=  "/web/uc/order/initPayOrder?orderType=COMMON&shopData="+$(this).val()+"-BOOK-1";
    });

    /**
     * 图书详情
     * @param obj
     */
    function bookDetails(obj) {
        var bookId = obj.parents('article').data('book');
        window.location = baseloaction + '/web/book/info?bookId=' + bookId;
    }

    /**
     * 排序选择
     * @param obj
     */
    function selectOrder(obj) {
        var order = obj.data('order');
        if(order==2){
            obj.data('order',4);
        }
        if(order==4){
            obj.data('order',2);
        }
        $('span[data-order]').find('a').removeClass('c-danger')
        obj.find('a').addClass('c-danger');
        $('input[name="order"]').val(order);
        listData(1);
    }

    /**
     * 子专业选择
     * @param obj
     */
    function selectSubSubject(obj) {
        var subjectId = obj.data('id');
        obj.addClass('active').siblings().removeClass('active');
        $("#subjectId").val(subjectId);
        listData(1);
    }

    /**
     * 加载第 pageNo 页数据
     * @param pageNo
     */
    function listData(pageNo) {
        var params = $('#bookForm').serializeJSON();
        params = JSON.stringify(params);
        getPageAjaxData({
            url: baseloaction + '/web/book/ajaxBookList',
            contextId: 'book-list-container',
            pageNo: pageNo,
            pageCall: listData,
            params: params,
            callback: function (pageNo) {
                $('#count').text($("#book-total").val());
            }
        });
        lazyBookImage();
    }

});